<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="keywords"
            placeholder="请输入关键词"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
            @change="getDeptTree"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptList"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-button @click="downZip">批量下载</el-button>
        <el-table
          v-loading="loading"
          :data="dataList"
          @selection-change="handleSelectionChange"
          style="margin-top: 20px"
        >
          <el-table-column
            type="selection"
            width="50"
            align="center"
            :selectable="handleDisable"
          />
          <el-table-column type="index" width="50" align="center" label="序号" />
          <el-table-column label="楼层名称" align="center" prop="floorAlias" />
          <el-table-column label="文件名" align="center" prop="fileName" />
          <el-table-column label="大小" align="center">
            <template slot-scope="scope" v-if="scope.row.fileSize">
              {{ scope.row.fileSize }}M
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >上传</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <Upload-regulate v-if="uploadShow" ref="uploadRegulate" @refresh="refresh" />
    <Form v-if="formShow" ref="Form" />
  </div>
</template>
<script>
import { buildingDrawingList, imageListBuild, imageZipDown } from "@/api/system/ai";
import { blobValidate } from "@/utils/ruoyi";
import { saveAs } from "file-saver";
import UploadRegulate from "./upload-regulate.vue";
import Form from "./form.vue";
export default {
  name: "drawing",
  components: { UploadRegulate, Form },
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      dataList: [], // 数据列表
      total: 0, // 总条数
      deptList: [], // 部门树
      keywords: null, // 关键词
      defaultProps: {
        children: "children",
        label: "name",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingId: null,
      },
      uploadShow: false, // 上传弹窗
      formShow: false, // 编辑弹窗
    };
  },
  created() {
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      this.loading = true;
      buildingDrawingList({ keywords: this.keywords }).then((res) => {
        this.deptList = res.data;
        if (res.data.length) {
          this.queryParams.buildingId = res.data[0].id;
          this.getList();
        }
      });
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      imageListBuild(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) != -1;
    },
    // 是否禁用多选
    handleDisable(row) {
      return row.fileName;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.buildingId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    refresh() {
      this.uploadShow = false;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.floorId);
    },

    /** 上传按钮操作 */
    handleUpdate(row) {
      this.uploadShow = true;
      this.$nextTick(() => {
        this.$refs.uploadRegulate.init(row, this.queryParams.buildingId);
      });
    },
    /** 编辑按钮操作 */
    handleDelete(row) {
      this.formShow = true;
      this.$nextTick(() => {
        this.$refs.Form.init(row);
      });
    },
    downZip() {
      if (this.ids.length <= 0) {
        this.$modal.msgError("请选择要下载的文件");
        return;
      }
      imageZipDown(this.ids).then(async (data) => {
        const isBlob = blobValidate(data.data);
        const filename = decodeURIComponent(data.filename);
        if (isBlob) {
          saveAs(data.data, filename);
        } else {
          const resText = await data.text();
          const rspObj = JSON.parse(resText);
          const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
          this.$modal.msgError(errMsg);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.infoConstruction {
  height: 2rem;
  width: 100%;
  line-height: 2rem;
  font-size: 16px;
  font-weight: bold;
  // color: #8c939d;
  text-align: center;
  background: #8c939d;
}

.pointerAcc {
  pointer-events: none;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.select-tree {
  .el-select-dropdown__item {
    padding: 5px 10px;
    height: auto;
    overflow: hidden;
  }

  .el-select-dropdown__item.hover,
  .el-select-dropdown__item:hover {
    background-color: #fff;
  }

  .el-select-dropdown__item.selected {
    font-weight: normal;
  }
}
</style>
